package img

import (
	"common/mildware"
	"common/models"
	"common/validate"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
)

// SearchImgByAuthorId 搜索图片
// @Summary 搜索图片
// @Description 搜索图片
// @Tags 图片
// @Accept application/json
// @Produce application/json
// @Param Authorization header string true "Bearer 用户令牌"
// @Param object body docModel.DocSearchImgByAuthorId false "搜索参数"
// @Security ApiKeyAuth
// @Success 200 {object} docModel.DocResp
// @Router  /img/search [post]
func (h IHandler) SearchImgByAuthorId(c *gin.Context) {
	var (
		authorId string
		err      error
		res      []*models.ImgInfo
	)
	resp := new(models.RespData)
	req := new(models.SearchImgByAuthorId)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("Img:SearchImgByAuthorId with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("Img:SearchImgByAuthorId with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	//获得uid
	authorId, _, _, err = mildware.GetCyrrentUserID(c)
	if err != nil {
		resp.SendCode(c, models.CodeInvalidToken)
		return
	}
	list, total, err := h.M.ImgInfoQueryByAuthId(authorId, req.Page, req.Size)
	if err != nil {
		zap.L().Error("Img :h.M.ImgInfoQueryByAuthId error", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	for _, info := range list {
		demo := &models.ImgInfo{
			Info:      info,
			ImageHttp: models.GetHttpUrl(info.ImageId, models.HttpImg),
		}
		res = append(res, demo)
	}
	resp.Count = total
	resp.Data = res
	resp.SendSuccess(c)
}
